//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;


public class UI_Inventory : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	int activePanels;
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	public Rect m_scrollContent;
	public GameObject m_itemPanel;
	public GameObject m_scrollPanel;
	public List<UI_Item> m_inventoryPanels;
	public Inventory m_playerInventory;
	public Character m_playerMain;
	public Text m_maxCarry;
	public Text m_carry;
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	void Start()
	{
		// add UI item panels to list.
		foreach (Transform child in m_scrollPanel.transform)
		{
			m_inventoryPanels.Add(child.GetComponent<UI_Item>());
			
		}
	
	}
	
	//---------------------------------------------------------------------------------------------
	
	public void ShowWindow ()
	{
		

		// add items from player inventory.
		for (int a = 0; a < m_playerInventory.m_inventoryItems.Count; a++)
		{
			GameObject Panel = Instantiate(m_itemPanel, Vector3.zero, Quaternion.identity) as GameObject;
			
			Panel.GetComponent<RectTransform>().SetParent(m_scrollPanel.transform, false);
			m_inventoryPanels.Add(Panel.GetComponent<UI_Item>());
			
			
			Panel.GetComponent<UI_Item>().m_item = m_playerInventory.m_inventoryItems[a];
		}
		
		
		for (int a = 0; a < m_playerInventory.m_inventoryItems.Count; a++)
		{
			m_inventoryPanels[a].m_item = m_playerInventory.m_inventoryItems[a];
		}
		
		// check to see if item quantity is higher than 0 and show if relevant.
		if (m_inventoryPanels.Count > 0)
		{
			for (int a = 0; a < (m_playerInventory.m_inventoryItems.Count); a ++)
			{
				if (m_playerInventory.m_inventoryItems.Count > 0)
				{
					if (m_playerInventory.m_inventoryItems[a].Quantity == 0)
					{
						m_inventoryPanels[a].gameObject.SetActive(false);
					}
					if (m_playerInventory.m_inventoryItems[a].Quantity > 0)
					{
						m_inventoryPanels[a].gameObject.SetActive(true);
						activePanels ++;
					}
				}
			}
		}
		
		
		// attempt to set height based on amount of active panels.
		m_scrollContent.height = activePanels * 102;
		
	
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	void Update () 
	{
		m_maxCarry.text = m_playerMain.maxCarry.ToString();
		m_carry.text = m_playerMain.carry.ToString();
	}
	
	
	//---------------------------------------------------------------------------------------------
	public void CloseWindow()
	{
		for (int a = 0; a < m_inventoryPanels.Count; a++)
		{
			
			Destroy (m_inventoryPanels[a].gameObject);
		}
		m_inventoryPanels.RemoveRange(0 , m_inventoryPanels.Count);
	}
	
}
